package com.zretc.notice.controller;

import com.alibaba.fastjson.JSON;
import com.zretc.api.pojo.Notice;
import com.zretc.common.constant.SecurityConstant;
import com.zretc.common.entity.R;
import com.zretc.common.entity.RespResult;
import com.zretc.notice.service.NoticeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/notice")
public class NoticeController {
    @Autowired
    private NoticeService noticeService;
    @GetMapping("/queryNoticesTitleAndDateByAmount")
    public RespResult queryNoticesTitleAndDateByAmount(@RequestParam("index")Integer index, @RequestParam("pageNum")Integer pageNum)
    {
        List<Notice> list=noticeService.queryNoticesByAmount(index, pageNum);
        if(CollectionUtils.isEmpty(list))return R.fail(500,"服务器未找到数据或异常");
        List<Map<String, Object>> result = list.stream()
                .map(obj -> {
                    Map<String, Object> map = new HashMap<>();
                    // 假设MyObject类有getName和getAge方法
                    map.put("id", obj.getId());
                    map.put("title", obj.getTitle());
                    map.put("isTopped", obj.isTopped());
                    map.put("updateAt", obj.getUpdateAt());
                    // 为其他属性添加键值对
                    return map;
                })
                .collect(Collectors.toList());
        return R.success(result);
    }
    @GetMapping("/queryNoticeById")
    public RespResult queryNoticeById(@RequestParam("id")Integer id)
    {
        Notice notice=noticeService.queryNoticeById(id);
        if(Objects.isNull(notice))return R.fail(500,"服务器错误");
        Map<String,Object> result=new HashMap<>();
        result.put("id",notice.getId());
        result.put("title",notice.getTitle());
        result.put("content",notice.getContent());
        result.put("createBy",notice.getCreatedBy());
        result.put("createAt",notice.getCreatedAt());
        result.put("updateAt",notice.getUpdateAt());
        result.put("updateBy",notice.getUpdateBy());
        return R.success(result);
    }

    @PostMapping("/publishNotice")
    public RespResult publishNotice(@RequestBody Notice notice)
    {
        if(!noticeService.publishNotice(notice))return R.fail(500,"服务器错误");
        return R.success(null);
    }
    @PostMapping("/deleteNotice")
    public RespResult deleteNotice(@RequestBody Map<String,Object> map)
    {
        Integer id= (Integer) map.get("id");
        Integer updateBy= (Integer) map.get("updateBy");
        String dateString= (String) map.get("updateAt");
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        Date updateAt=Date.from(LocalDate.parse(dateString, formatter)
                .atStartOfDay(ZoneId.systemDefault()).toInstant());
        if(!noticeService.deleteNotice(id,updateAt,updateBy))return R.fail(500,"服务器错误");
        return R.success(null);
    }
    @PostMapping("/topNotice")
    public RespResult topNotice(@RequestBody Map<String,Object> map)
    {
        Integer id= (Integer) map.get("id");
        boolean isTopped= (boolean) map.get("isTopped");
        if(!noticeService.topNotice(id,isTopped))return R.fail(500,"置顶失败");
        return R.success(null);
    }
    @PostMapping("/updateNotice")
    public RespResult updateNotice(@RequestBody Map<String,Object> map, HttpServletRequest req)
    {
        map.put("updatedBy",Integer.valueOf(req.getHeader(SecurityConstant.AUTHENTICATION_KEY)));
        String json= JSON.toJSONString(map);
        Notice notice=JSON.parseObject(json,Notice.class);
        if(!noticeService.updateNotice(notice))return R.fail(500,"修改失败");
        return R.success(null);
    }
}
